package com.atguigu;

import com.atguigu.bean.UserBean;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.function.MapFunction;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Encoders;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import scala.collection.Map;

/**
 * @author yhm
 * @create 2022-12-26 8:54
 */
public class Test01_Method {
    public static void main(String[] args) {
        // 1. 创建sparkConf配置对象
        SparkConf conf = new SparkConf().setAppName("sql").setMaster("local[*]");

        // 2. 创建sparkSession连接对象
        SparkSession spark = SparkSession.builder().config(conf).getOrCreate();

        // 3. 编写代码
        Dataset<Row> dataset = spark.read().json("input/user.json");

        dataset.printSchema();
        dataset.show();


        Dataset<UserBean> userDS = dataset.as(Encoders.bean(UserBean.class));

        // 计算使用的时候 推荐使用javaBean的数据类型
        // 填写两个参数  还要再写一个返回类型的泛型
        Dataset<UserBean> dataset1 = userDS.map(new MapFunction<UserBean, UserBean>() {
            @Override
            public UserBean call(UserBean value) throws Exception {
                value.setAge(value.getAge() + 1);
                return value;
            }
        }, Encoders.bean(UserBean.class));

        // 4. 关闭sparkSession
        spark.close();
    }
}
